<?php
class Autoreply_model extends CI_Model {

    var $table_rule   = 'reply_rule';
    var $table_keyword   = 'reply_keyword';
    var $table_type = 'reply_type';

    function __construct()
    {
        parent::__construct();
    }
    
    function get_rule_list($where = '', $cond = array(), $offset = 0, $limit = 20){
    	if (!empty($where)){
    		$where = 'where ' . $where;
    	}
        $sql = 'select r.id, r.rule_name, r.type as rtype, r.material_id, r.material_type, r.content, r.add_time, k.keyword, k.add_time as k_add_time, m.type, m.title from '
        	. "{$this->table_rule} as r left join {$this->table_keyword} as k on k.rule_id=r.id left join material as m on m.id=r.material_id"
        	. " {$where} order by r.id desc limit {$offset}, {$limit}";
		$re_obj = $this->db->query($sql, $cond);
		
		if ($re_obj->num_rows() > 0){
			$re = $re_obj->result_array();
			$re_obj->free_result();
		}else {
			$re = array();
		}
		return $re;
    }
    
    function add_rule($items = array()){
    	$sql = "insert into {$this->table_rule} (rule_name, type, material_id, material_type, content, add_time)" . " values(?, ?, ?, ?, ?, ?)";
    	$this->db->query($sql, $items);
    	return $this->db->insert_id();
    }
    
    function update_rule($items = array(), $where = '', $cond = array()){
    	$_cond = array();
    	foreach ($items as $field => $val){
    		if (!isset($ups)){
    			$ups = "`{$field}`=?";
    		}else {
    			$ups .= ", `{$field}`=?";
    		}
    		$_cond[] = $val;
    	}
    	$_cond = array_merge($_cond, $cond);
    	$sql = "update {$this->table_rule} set {$ups} {$where}";
    	return $this->db->query($sql, $_cond);
    }
    
    function del_rule($where = '', $cond = array()){
    	if (empty($where)){
    		return false;
    	}
    	$sql = "delete from {$this->table_rule} where {$where}";
    	return $this->db->query($sql, $cond);
    }
    
    function get_keyword($where = '', $cond = array()){
    	if (!empty($where)){
    		$where = 'where ' . $where;
    	}
        $sql = 'select id, rule_id, keyword, add_time from '
        	. "{$this->table_keyword} {$where} order by id desc";
		$re_obj = $this->db->query($sql, $cond);
		
		if ($re_obj->num_rows() > 0){
			$re = $re_obj->result_array();
			$re_obj->free_result();
		}else {
			$re = array();
		}
		return $re;
    }
    
    function add_keyword($items = array()){
    	$sql = "insert into {$this->table_keyword} (rule_id, `keyword`, add_time)" . " values(?, ?, ?)";
    	return $this->db->query($sql, $items);
    }
    
    function del_keyword($where = '', $cond = array()){
    	if (empty($where)){
    		return false;
    	}
    	$sql = "delete from {$this->table_keyword} where {$where}";
    	return $this->db->query($sql, $cond);
    }
    
    function get_type_list($where = '', $cond = array()){
    	if (!empty($where)){
    		$where = 'where ' . $where;
    	}
        $sql = 'select id, label, orderby, add_time, status from '
        	. "{$this->table_type} {$where} order by orderby, id desc";
		$re_obj = $this->db->query($sql, $cond);
		
		if ($re_obj->num_rows() > 0){
			$re = $re_obj->result_array();
			$re_obj->free_result();
		}else {
			$re = array();
		}
		return $re;
    }
    
    function add_type($items = array()){
    	$sql = "insert into {$this->table_type} (label, add_time)" . " values(?, ?)";
    	return $this->db->query($sql, $items);
    }

}
